function partID=loginParticipant(expID,varargin)
%LOGINPARTICIPANT description.
%
% .... allowParticipantCreation?? Add authentication in FULL mode
% password dialog:
% http://www.mathworks.com/matlabcentral/fileexchange/19729
%
% Example: none
%
% See also
% 
% @Author: Pete R Jones
% @Date: 22/01/10

    %----------------------------------------------------------------------
    p = inputParser;   % Create an instance of the class.
    p.addRequired('experimentID',@ischar);
    p.addOptional('allowNewParticipants', false, @islogical);
    p.addParamValue('retries', 2, @(x)x>0 & mod(x,1)==0);
    p.FunctionName = 'LOGINPARTICIPANT';
    p.parse(expID,varargin{:}); % Parse & validate all input args
    expID=p.Results.experimentID;
    allowNewParticipants=p.Results.allowNewParticipants;
    numOfRetriesAllowed=p.Results.retries;
    %----------------------------------------------------------------------
    inFullMode=strcmpi(getPrefVal('mode'),'full');
    %----------------------------------------------------------------------
       
    % Check good to go
    if ~isValidExpID(expID)
        error('PsychTestRig:loginParticipant',['Invalid Experiment ID: ' expID])
    end
     
                
    % Run
    % If any options other than log-in are available, present the user with a choice
    if (allowNewParticipants)
        resp=get1ofMInput('Login participant (l), register participant (r), create new participant (c)? ',{'l','r','c'});
        if strcmp(resp,'l')
            sub_logInParticipant();
        elseif strcmp(resp,'r')
            sub_registerParticipant();
        elseif strcmp(resp,'c')
            sub_createParticipant();
        else
            error('PsychTestRig:loginParticipant:logicError','!!Programming error!!');
        end
    else
        local_logInParticipant();
    end
    


    %%%%%%%%%%%%%%%%%%%%
    %%% SUBFUNCTIONS %%%
    %%%%%%%%%%%%%%%%%%%%
    
     function sub_logInParticipant()
        tries=0;
        while (1)
            id=num2str(getIntegerInput('Participant ID: ',false,0));
            if ~isempty(id)
                if isValidPartID(expID, id)
                    break
                end
            end
            tries=tries+1;
            if (tries > numOfRetriesAllowed)
                error(['Login Failed: Maximum number of retries (' int2str(numOfRetriesAllowed) ') reached'])
            end
            disp(' ')
        end
        cloutput('log in successful.\n')

        if (inFullMode) %if running in full mode
            ammendParticipantRecord(id)
            % command-line alternative: 
            %get participant record details
            %display participant details
            % % % %         [name notes]=mysql(['SELECT name, notes FROM participants WHERE name = "' id '";']);
            % % % %         info = struct('name', name, 'notes', notes);      
            % % % %         cloutput('%line')
            % % % %         disp(' ')
            % % % %         disp(info)
            % % % %         cloutput('%line')
            % % % %         correct=getBooleanInput('Are these details
            % correct? (y/n) ');
            % % % %         if (~correct)
            % % % %             cloutput('DANGER! DANGER! DANGER!')
            % % % %         end      
        end


        partID=id;
    end

    function sub_registerParticipant()
        error('>>>>>PLEASE WRITE SOME CODE HERE!!!!<<<<<')
        %assign participant to experiment
    end

    function sub_createParticipant()
        error('>>>>>PLEASE WRITE SOME CODE HERE!!!!<<<<<')
        %create participant in central db
        %assign participant to experiment
    end

end


%%%%%%%%%%%%%%%%%%%%%%%
%%% LOCAL FUNCTIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%

